/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.bioingenium.kbmed.data.loader;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alejandro Riveros Cruz
 */
public class DocumentLoaderFactory {

    private static Map<String, DocumentLoader> instanceMap = new HashMap<String, DocumentLoader>();

    /**
     * 
     * @param fileName
     * @return
     * @throws ClassNotFoundException
     */
    public static DocumentLoader getDocumentLoader(String fileName) {
        String extension = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();
        if (instanceMap.containsKey(extension)) {
            return instanceMap.get(extension);
        } else {
            try {
                DocumentLoader documentLoader;
                documentLoader = (DocumentLoader) Class.forName("co.edu.unal.bioingenium.kbmed.data.loader." + extension.toUpperCase() + "Loader").newInstance();
                instanceMap.put(extension, documentLoader);
                return documentLoader;
            } catch (InstantiationException ex) {
                Logger.getLogger(DocumentLoaderFactory.class.getName()).log(Level.SEVERE, "Error while instance the loader.", ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(DocumentLoaderFactory.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(DocumentLoaderFactory.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }
}
